﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using UnityEngine.Events;

public class UIDropList : MonoBehaviour
{
    public Transform contentLayout;
    public RectTransform contentArea;
    [SerializeField]
    private Button m_Button = null;

    /// <summary>
    /// Tab 标签切换事件，请处理标签的切换状态
    /// </summary>
    [System.Serializable]
    public class DisplayChangeEvent : UnityEvent<bool> { };
    //标签切换回调函数，标签切换时使用，控制一个
    public DisplayChangeEvent onChangeDisplay;

    private Vector2 _dropListSize;

    public bool isShow { get; private set; }
    private bool isMoving = false;

    public float minSize = 128f;

    private void Awake()
    {
        this._dropListSize = this.contentArea.sizeDelta;
        this.contentArea.sizeDelta = new Vector2(this.contentArea.sizeDelta.x, minSize);
        this.m_Button?.onClick?.AddListener(OnClick);
        isShow = false;
    }

    private void OnDestroy() {
        DOTween.Kill(gameObject);
    }

    public void OnClick()
    {
        if (isMoving) return;
        AudioManager.Instance.Play("btn_click");

        float duration = 0.4f;
        this.onChangeDisplay?.Invoke(isShow);
        if (this.isShow == true)
        {
            isMoving = true;
            DOTween.To(() => this.contentArea.sizeDelta.y, (a) => this.contentArea.sizeDelta = new Vector2(this.contentArea.sizeDelta.x, a), minSize, duration)
            .SetId(gameObject)
            .onComplete += () =>
            {
                isMoving = false;
                isShow = false;
            }
            ;
        }
        else
        {
            isMoving = true;
            DOTween.To(() => this.contentArea.sizeDelta.y, (a) => this.contentArea.sizeDelta = new Vector2(this.contentArea.sizeDelta.x, a), this._dropListSize.y, duration)
            .SetId(gameObject)
            .onComplete += () =>
            {
                isMoving = false;
                isShow = true;
            }
            ;
        }
    }

}
